/* down.phtml JS */

var xindm = {
	/* 信息存储 */
	"info" : {}, // {"fail": "0", "preCount": "0", "timer": "0"}
	/* 下载状态 */
	"end": function (bool) {
		var msg = bool ? '下载完成 !' : '下载失败 ! <a href="javascript:location.href = location.href;" title="Retry">重试</a>'
		$('#wait')[0].innerHTML = msg;
		window.clearInterval(this.info.timer);
	},
	/* 获取下载进度 */
	"checkProgress": function () {
		var s = this;
		var url = '?a=checkDown&n=' + s.info.comicName + '&c=1' + '&dataPath=' + s.info.dataPath + '&lists=' + s.info.lists;				
		s.info.timer = setInterval(function () {
			if (!s.info.status) {
				return false;
			}
			$.get(url, function (_data) {
				var data = typeof JSON != 'undefined' ? JSON.parse(_data) : eval('(' + _data + ')');
				//console.log(data);
				if (typeof data.status != 'undefined') {
					s.dealProgress(data);
					return true;
				}
				console.log('error: source data[' + _data + ']');
			});
		}, 2500);
	},
	/* 更新进度信息 */
	"dealProgress": function (data) {
		var s = this;
		if (data.status == 'true') {
			s.info.fail = s.info.preCount == data.downCount ? ++s.info.fail : 0;
			if (data.downCount > s.info.preCount) {
				$('#picDown').text(data.downCount);
				$('#progress').val(data.downCount);
				$('#listDown').text(data.listDownCount);
			}
			
			var increase = data.downCount - s.info.preCount;
			increase = increase > 0 ? increase : '0';
			$('#increase').html('<span>+' + increase + '</span>'); // 增量动画
			s.info.preCount = data.downCount;
			
			var listProgress = data.listDownProgress;
			/*var listcomplateArr = data.listComplateNum != '0' ? data.listComplateNum.split(',') : false;
			if (listcomplateArr) {
				for (var l in listcomplateArr) {
					//$('#vol_' + listcomplateArr[l]).addClass('complate');
				}
			}*/
			for (var ii in listProgress) {
				//, 
				$('#vol_' + listProgress[ii].vol).attr({"title": listProgress[ii].volName + '(' + listProgress[ii].down + '/' + listProgress[ii].total + ')'});
				var cat = '#vol_' + listProgress[ii].vol + ' progress:eq(0)';
				var item = $(cat);
				item.attr("max", listProgress[ii].total).val(listProgress[ii].down);
				listProgress[ii].down > 0 && item.show();
			}
			if (data.downCount == s.info.picCount) {
				s.end(true);
				return true;
			}
		} else if (data.status == 'false') {
			++s.info.fail;
		}
		if (s.info.fail >= 30) {
			console.log('time out');
			s.end(false);
		}
	},
	/* 信息初始化 */
	"init": function (info) {
		if (typeof info == 'undefined') {
			alert('信息初始化失败··');
			return false;
		}
		this.info = info;
		this.info.status = true;
		this.info.preCount = 0;
		this.info.fail = 0;
		this.info.timer = 0;
		var titleContent = '<a style="color: #696A52;" title="跳转漫画主页" href="http://www.jide123.com/manhua/' + info.comicName + '" target="_blank">' + info.comicTitle + '</a>';
		$('#comicTitle').html(titleContent);
		$('#picCount').text(info.picCount);
		$('#progress').attr('max', info.picCount);
		$('#listCount').text(info.listCount);
		var volHtml = this.makeVolHtml(info.lists);
		$('#listNum').html(volHtml);
		//$('#listNum').attr('title', info.lists);
		$('#checkFile')[0].href = info.checkFile;
		$('#zipPackage')[0].href = info.zipPackage;
		this.checkProgress();
		$('#control').show(); // 下载控制
	},
	/* 生成卷集html */
	"makeVolHtml": function (volFoo) {
		var volStr = volFoo;
		var volArr = volStr.split(',');
		var volHtmlArr = new Array();
		for (var i in volArr) {
			var progress = '<progress max="100" value="0" class="progress_vol"></progress>';
			volHtmlArr.push('<span class="volNum" id="vol_' + volArr[i] + '">' + volArr[i] + progress + '</span>');
		}
		var fengefu = '<span class="fengefu">,</span>';
		var volHtml = volHtmlArr.join(fengefu);
		return volHtml;
	},
	/* 暂停/开始 */
	"control": function () {
		var control = $('#control'), s = this;
		if (s.info.status) {
			$.get('?a=downloadControl&comic=' + s.info.comicName, function (_data) {
				try {
					var data = eval('(' + _data + ')');
					if (data.status == 'true') {
						s.info.status = false;
						control.text('继续') + control.attr('title', 'start');
					} else {
						alert('操作失败！');
					}
				} catch(err) {
					alert('操作失败！未知错误');
				}
			});
		} else {
			$.get(location.href);
			s.info.status = true;
			control.text('暂停') + control.attr('title', 'stop');
		}
	}
}

/*
*/